package com.zxl.grocery.core.webmagic.pipeline;

import com.zxl.grocery.common.persistence.model.CrawlerFunny;
import com.zxl.grocery.modular.crawler.service.ICrawlerFunnyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * info:笑话
 * Created by Lzx on 2017/7/12.
 */
@Repository
public class FunnyPipeline implements Pipeline {
    @Autowired
    private ICrawlerFunnyService iCrawlerFunnyService;

    @Override
    public void process(ResultItems resultItems, Task task) {
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            if (entry.getKey().equals("funny")) {
                CrawlerFunny funny=(CrawlerFunny) entry.getValue();
                HashMap<String, Object> conditionMap = new HashMap<String, Object>();
                conditionMap.put("title",funny.getTitle());
                List<CrawlerFunny> funnies = iCrawlerFunnyService.selectByMap(conditionMap);
                if (funnies.size() < 1) {//检查标题是否已存在
                    funny.setTypeId(1);
                    funny.setAuthor("水花");
                    funny.setCreateDate(new Date());
                    funny.setStatus(1);
                    funny.setModifyDate(new Date());
                    iCrawlerFunnyService.insert(funny);
                }
            }

        }
    }
}
